package com.xyy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xyy.common.PageResult;
import com.xyy.common.Result;
import com.xyy.entity.Activity;
import com.xyy.entity.DoctorActivityRegistration;
import com.xyy.service.DoctorActivityRegistrationService;
import com.xyy.service.PatientService;
import com.xyy.vo.DoctorActivityVO;
import com.xyy.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 医生活动报名控制器
 */
@RestController
@RequestMapping("/doctor/activity")
@CrossOrigin
public class DoctorActivityRegistrationController {
    
    @Autowired
    private DoctorActivityRegistrationService doctorActivityRegistrationService;
    
    @Autowired
    private PatientService patientService;
    
    @Autowired
    private JwtUtil jwtUtil;
    
    /**
     * 获取医生可报名的活动列表（医生附属管理员发布的活动）
     */
    @GetMapping("/available")
    public Result<PageResult<Activity>> getAvailableActivities(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            HttpServletRequest request) {
        
        // 从JWT中获取医生ID
        Long doctorId = jwtUtil.getUserIdFromRequest(request);
        
        IPage<Activity> page = doctorActivityRegistrationService
            .getAvailableActivitiesForDoctor(doctorId, current, size);
        
        PageResult<Activity> pageResult = new PageResult<>();
        pageResult.setRecords(page.getRecords());
        pageResult.setTotal(page.getTotal());
        pageResult.setCurrent(page.getCurrent());
        pageResult.setSize(page.getSize());
        
        return Result.success(pageResult);
    }
    
    /**
     * 获取医生已报名的活动列表
     */
    @GetMapping("/registered")
    public Result<PageResult<DoctorActivityVO>> getRegisteredActivities(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            HttpServletRequest request) {
        
        // 从JWT中获取医生ID
        Long doctorId = jwtUtil.getUserIdFromRequest(request);
        
        IPage<DoctorActivityVO> page = doctorActivityRegistrationService
            .getRegisteredActivitiesWithStatusByDoctor(doctorId, current, size);
        
        PageResult<DoctorActivityVO> pageResult = new PageResult<>();
        pageResult.setRecords(page.getRecords());
        pageResult.setTotal(page.getTotal());
        pageResult.setCurrent(page.getCurrent());
        pageResult.setSize(page.getSize());
        
        return Result.success(pageResult);
    }
    
    /**
     * 医生报名活动
     */
    @PostMapping("/register/{activityId}")
    public Result<String> registerActivity(
            @PathVariable Long activityId,
            @RequestParam(required = false) String remark,
            HttpServletRequest request) {
        
        // 从JWT中获取医生ID
        Long doctorId = jwtUtil.getUserIdFromRequest(request);
        
        boolean success = doctorActivityRegistrationService
            .registerActivity(activityId, doctorId, remark);
        
        if (success) {
            return Result.success("报名成功");
        } else {
            return Result.error("报名失败");
        }
    }
    
    /**
     * 医生取消报名
     */
    @PostMapping("/cancel/{activityId}")
    public Result<String> cancelRegistration(
            @PathVariable Long activityId,
            HttpServletRequest request) {
        
        // 从JWT中获取医生ID
        Long doctorId = jwtUtil.getUserIdFromRequest(request);
        
        boolean success = doctorActivityRegistrationService
            .cancelRegistration(activityId, doctorId);
        
        if (success) {
            return Result.success("取消报名成功");
        } else {
            return Result.error("取消报名失败，可能您未报名此活动");
        }
    }
    
    /**
     * 统计医生参加过的活动数量
     */
    @GetMapping("/attended/count")
    public Result<Integer> getAttendedActivitiesCount(HttpServletRequest request) {
        // 从JWT中获取医生ID
        Long doctorId = jwtUtil.getUserIdFromRequest(request);
        
        Integer count = doctorActivityRegistrationService
            .countAttendedActivitiesByDoctor(doctorId);
        
        return Result.success(count);
    }
    
    /**
     * 获取医生总积分
     */
    @GetMapping("/points/total")
    public Result<Integer> getTotalPoints(HttpServletRequest request) {
        // 从JWT中获取医生ID
        Long doctorId = jwtUtil.getUserIdFromRequest(request);
        
        Integer totalPoints = doctorActivityRegistrationService
            .getTotalPointsByDoctor(doctorId);
        
        return Result.success(totalPoints);
    }
    
    /**
     * 获取医生工作统计信息
     */
    @GetMapping("/work-statistics")
    public Result<com.xyy.vo.DoctorWorkStatisticsVO> getWorkStatistics(HttpServletRequest request) {
        // 从JWT中获取医生ID
        Long doctorId = jwtUtil.getUserIdFromRequest(request);
        
        // 获取本医院全部活动数量
        Integer totalActivities = doctorActivityRegistrationService.countAllActivitiesByHospital(doctorId);
        
        // 获取健康档案统计数据（全部健康档案）
        Integer healthRecords = patientService.countHealthRecordsByDoctor(doctorId);
        
        // 创建统计VO对象
        com.xyy.vo.DoctorWorkStatisticsVO statistics = new com.xyy.vo.DoctorWorkStatisticsVO(
            totalActivities, healthRecords
        );
        
        return Result.success(statistics);
    }
}
